package com.hanserwei.han_picture.config;

import com.hanserwei.han_picture.websocket.AiProcessWebSocketHandler;
import com.hanserwei.han_picture.websocket.PictureEditHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置
 *
 * @author hanserwei
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final AiProcessWebSocketHandler aiProcessWebSocketHandler;
    private final PictureEditHandler pictureEditHandler;
    private final WebSocketHandshakeInterceptor webSocketHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(@NonNull WebSocketHandlerRegistry registry) {
        // AI 批处理通道不需要额外握手校验
        registry.addHandler(aiProcessWebSocketHandler, "/ws/ai-process")
                .setAllowedOrigins("*"); // 生产环境应该限制具体的域名

        // 协同编辑通道需要校验图片相关信息
        registry.addHandler(pictureEditHandler, "/ws/picture/edit")
                .addInterceptors(webSocketHandshakeInterceptor)
                .setAllowedOrigins("*"); // 生产环境应该限制具体的域名
    }
}
